package com.yboot.framework.plugin.generator.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author .
 * @date 2022-11-10
 */
public class TypeUtils {

    /**
     * key: jdbcType
     * value: javaType
     */
    private static Map<String, String> typeMap = new HashMap<>();

    /**
     * key: jdbcType
     * value: validator name
     */
    private static Map<String, String> validatorMap = new HashMap<>();

    static {
        typeMap.put("INT", "Integer");
        typeMap.put("CHAR", "String");
        typeMap.put("VARCHAR", "String");
        typeMap.put("LONGVARCHAR", "String");
        typeMap.put("BIT", "Boolean");
        typeMap.put("BOOLEAN", "Boolean");
        typeMap.put("TINYINT", "String");
        typeMap.put("SMALLINT", "Short");
        typeMap.put("INTEGER", "Integer");
        typeMap.put("BIGINT", "Long");
        typeMap.put("REAL", "Float");
        typeMap.put("FLOAT", "Double");
        typeMap.put("DOUBLE", "Double");
        typeMap.put("BINARY", "byte[]");
        typeMap.put("VARBINARY", "byte[]");
        typeMap.put("LONGVARBINARY", "byte[]");
        typeMap.put("DATE", "Date");
        typeMap.put("TIME", "Date");
        typeMap.put("TIMESTAMP", "Date");
        typeMap.put("DATETIME", "LocalDateTime");
        typeMap.put("CLOB", "Clob");
        typeMap.put("BLOB", "Blob");
        typeMap.put("ARRAY", "Array");

        validatorMap.put("String", "@NotBlank");
        validatorMap.put("Boolean", "@NotNull");
        validatorMap.put("byte", "@NotBlank");
        validatorMap.put("Short", "@NotNull");
        validatorMap.put("Integer", "@NotNull");
        validatorMap.put("Long", "@NotNull");
        validatorMap.put("Float", "@NotNull");
        validatorMap.put("Double", "@NotNull");
        validatorMap.put("byte[]", "@NotBlank");
        validatorMap.put("Date", "@NotNull");
        validatorMap.put("Clob", "@NotNull");
        validatorMap.put("Blob", "@NotNull");
        validatorMap.put("Array", "@NotBlank");

    }

    public static String getJavaType(String jdbcType) {
        String upper = jdbcType.toUpperCase();
        if (typeMap.containsKey(upper)) {
            return typeMap.get(upper);
        }
        return "String";
    }

    public static String getValidator(String javaType) {
        if (validatorMap.containsKey(javaType)) {
            return validatorMap.get(javaType);
        }
        return "@NotNull";
    }

}
